package com.example.meitao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.meitao.R;
import com.example.meitao.bean.ChunWen;
import com.example.meitao.config.NetConfig;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by 星星 on 2016/8/13.
 */
public class ChunWenAdapter extends RecyclerView.Adapter<ChunWenAdapter.ViewHolder>{
    private List<ChunWen.Itemsbean> data;
    private Context context;
    private ImageOptions options;
    public ChunWenAdapter(List<ChunWen.Itemsbean> data, Context context) {
        this.data = data;
        this.context = context;
        options = new ImageOptions.Builder().setCircular(true).build();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_wen,null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ChunWen.Itemsbean itemsbean = data.get(position);
        holder.tvContent.setText(itemsbean.getContent());
        ChunWen.Itemsbean.Userbean user = itemsbean.getUser();
        if(user!=null&&!TextUtils.isEmpty(user.getIcon())) {
            String path = String.format(NetConfig.URL_USER_ICON, user.getId() / 10000, user.getId(), user.getIcon());
            x.image().bind(holder.imageView,path,options);
        }
        if(user!=null&&!TextUtils.isEmpty(user.getLogin())) {
            holder.tvName.setText(user.getLogin());
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvName,tvContent;
        private ImageView imageView;
        public ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_pull_item_name);
            tvContent = (TextView) itemView.findViewById(R.id.tv_pull_item_content);
            imageView = (ImageView) itemView.findViewById(R.id.iv_pull_item_icon);

        }
    }
}
